<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper" v-if="createFormBox">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="4" :sm="24" style="min-width: 230px;">
            <a-form-item>
              <a-input v-model="queryParam.roleName" placeholder="请输入角色姓名" />
            </a-form-item>
          </a-col>
          <div style="float: right;margin-right:24px;">
            <a-button type="primary" icon="plus" size="20" @click="createFormBox=false" v-if="operationPermission['add']">创建角色</a-button>
          </div>
        </a-row>
      </a-form>
      <s-table
        class="table_style"
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        showPagination="auto">
        <span slot="serial" slot-scope="text,record,index">{{ index + 1 }}</span>
        <span slot="action" slot-scope="text,record,index" class="operation_button_box">
          <template>
            <a-button size="small" class="operation_button_passwd" @click="handleEdit(record)" v-if="operationPermission['update']">编辑</a-button>
          </template>
        </span>
      </s-table>
    </div>
    <div v-else>

      <h3 class="create_form_title">创建角色</h3>

      <div class="createFormEdit_content">
        <a-form :form="form">

          <a-form-item label="角色名称">
            <a-input v-decorator="['addRoleName', { rules: [{ required: true, message: '请输入角色名称!' }] }]" placeholder="请输入角色名称" />
          </a-form-item>
          <a-form-item label="角色描述">
            <a-input v-decorator="['addRoleDesc', { rules: [{ required: true, message: '请输入角色描述!' }] }]" placeholder="请输入角色描述" />
          </a-form-item>

        </a-form>

        <div class="createFormEdit_font_box">
          <div class="createFormEdit_font">
            <a-button size="large" key="cancel" @click="handleCancel" style="margin-left:20px;">取消</a-button>
            <a-button
              size="large"
              key="forward"
              :loading="confirmLoading"
              type="primary"
              @click="handleAdd()"
              style="margin-left:20px;">确定</a-button>

          </div>
        </div>

      </div>

    </div>
    <a-modal centered :width="976" :footer="null" v-model="createFormEdit" @ok="handleOk">
      <template slot="title">
        <p class="createFormEdit_title">编辑角色</p>
      </template>

      <div class="createFormEdit_content_modal">
        <a-form :form="form">

          <a-form-item label="角色名称">
            <a-input v-decorator="['selectedRoleName', { rules: [{ required: true, message: '请输入角色名称!' }] }]" placeholder="请输入角色名称" />
          </a-form-item>
          <a-form-item label="角色描述">
            <a-input v-decorator="['selectedRoleDesc', { rules: [{ required: true, message: '请输入角色描述!' }] }]" placeholder="请输入角色描述" />
          </a-form-item>

        </a-form>

        <div class="createFormEdit_font_box">
          <div class="createFormEdit_font">
            <a-button size="large" key="cancel" @click="handleCancel" style="margin-left:20px;">取消</a-button>
            <a-button
              size="large"
              key="forward"
              :loading="confirmLoading"
              type="primary"
              @click="handleUpdate()"
              style="margin-left:20px;">确定</a-button>

          </div>
        </div>

      </div>
    </a-modal>
    <!-- <test-design @ok="handleOk" ref="modal"></test-design>
    <step-by-step-modal ref="modal" @ok="handleOk" /> -->
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getRoleList, postRoleAdd, postRoleUpdate } from '@/api/manage'

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis
    // TestDesign
  },
  data () {
    return {
      operationPermission:{},
      selectedRoleId: '',
      selectedRoleName: '',
      selectedRoleDesc: '',
      createFormEdit: false,
      confirmLoading: false,
      createFormBox: true,
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      form: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 表头
      visible: false,
      designArray: {
        '0': '预发布',
        '1': '进行中',
        '2': '暂停中',
        '3': '已结束'
      },
      columns: [
        {
          title: '序号',
          // scopedSlots: { customRender: 'id' },
          // dataIndex: 'id',
          align: 'center',
          scopedSlots: {
            customRender: 'serial'
          }
        },
        {
          title: '角色',
          dataIndex: 'roleName',
          align: 'center'
        },
        {
          title: '描述',
          dataIndex: 'des',
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '200px',
          scopedSlots: { customRender: 'action' },
          align: 'center'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log('loadData.parameter', parameter)
        return getRoleList(Object.assign(parameter, this.queryParam))
          .then(res => {
            // console.log(res, "shuj")
            console.log(res.message)
            res.data.data = res.data.list
            return res.data
            // return res.result
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true
    }
  },
  filters: {
    statusFilter (type) {
      // return statusMap[type].text
    },
    statusTypeFilter (type) {
      // return statusMap[type].status
    }
  },
  created () {
    
    let list = JSON.parse(localStorage.getItem("operationPermission"))
    list.forEach(element => {
      if (element.permissionId == 'roleSetting') { 
        element.actionVOS.forEach(i => {
          this.operationPermission[i.action] = i.defaultCheck
        });
      }      
    });
    this.tableOption()
    // getRoleList({ t: new Date() })
  },
  methods: {
    CreateFormHandleCancel () {
      this.createFormBox = true
    },
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            getCheckboxProps: record => ({
              props: {
                disabled: record.no === 'No 2', // Column configuration not to be checked
                name: record.no
              }
            })
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleAdd () {
      // eslint-disable-next-line handle-callback-err
      this.form.validateFields((err, values) => {
        console.log(values)

        if (!values) {
          return
        }

        if (!values['addRoleName'] || !values['addRoleDesc']) {
          return
        }

        this.confirmLoading = true
        // this.createFormEdit = false
        this.confirmLoading = true
        const selectedRoleName = values['addRoleName']
        const selectedRoleDesc = values['addRoleDesc']

        const body = {
          roleName: selectedRoleName,
          des: selectedRoleDesc
        }

        // debugger
        postRoleAdd(body).then(res => {
          console.log(res.message)
          // this.roles = res.data
          // debugger
          // this.selectedRoles = res.data.roles
          // return res.data
          this.createFormBox = true
          this.confirmLoading = false
          this.$refs.table.refresh(true)
        })
      })
    },
    handleUpdate () {
      // this.confirmLoading = true
      // this.createFormEdit = false

      // eslint-disable-next-line handle-callback-err
      this.form.validateFields((err, values) => {
        console.log(values)

        if (!values) {
          return
        }

        if (!values['selectedRoleName'] || !values['selectedRoleDesc']) {
          return
        }

        this.confirmLoading = true
        const selectedRoleName = values['selectedRoleName']
        const selectedRoleDesc = values['selectedRoleDesc']

        const body = {
          id: this.selectedRoleId,
          roleName: selectedRoleName,
          des: selectedRoleDesc
        }

        // debugger
        postRoleUpdate(body).then(res => {
          console.log(res.message)
          // this.roles = res.data
          // debugger
          // this.selectedRoles = res.data.roles
          // return res.data
          this.createFormEdit = false
          this.confirmLoading = false
          this.$refs.table.refresh(true)
        })
      })
    },
    handleCancel () {
      this.createFormEdit = false
      this.confirmLoading = false
      this.createFormBox = true
    },
    handleEdit (record) {
      this.selectedRoleId = record.id
      // this.selectedRoleName = record.roleName
      // this.selectedRoleDesc = record.des
      this.createFormEdit = true
      setTimeout(() => {
        this.form.setFieldsValue({ selectedRoleDesc: record.des })
        this.form.setFieldsValue({ selectedRoleName: record.roleName })
      }, 100)

      // console.log(record)
      // this.$refs.modal.edit(record)
    },
    handleSub (record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    },
    tableStatusEvent (v) {
      // console.log(this.$refs.tableStatusEvent,"徐强ggg")
      // this.$refs.tableStatusEvent.onclick
      // this.visible = false
      console.log(this.visible, '徐强')
    },
    aaa () {

    }
  },
  watch: {
    queryParam: {
      handler (newValue, oldValue) {
        this.$refs.table.refresh(true)
      },
      deep: true
    }
  }
}
</script>

<style lang="less" scoped>
.table_status {
  margin: 5px;
}
.ant-popover-title {
  width: 98px;
  height: 22px;
  font-size: 14px;
  font-family: PingFangSC-Regular, Microsoft Yahei, sans-serif;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  line-height: 22px;
}
.createFormEdit_title {
  font-size: 24px;
  font-family: PingFangSC-Medium, Microsoft Yahei, sans-serif;
  font-weight: 700;
  color: rgba(0, 0, 0, 0.85);
  line-height: 24px;
  margin: 0px;
}
.createFormEdit_content{
  margin: 95px 66px;
}
.createFormEdit_content_modal{

  margin: 22px 66px 50px 66px;
}

.createFormEdit_title_box {
  margin-top: -30px;
}
.createFormEdit_font_box {
  height: 50px;
  .createFormEdit_font {
    float: right;
  }
}

.create_form_title {
  font-size: 24px;
  font-weight: bold;
  color: rgba(74, 74, 74, 1);
  line-height: 22px;
}

.ant-popover-inner-content {
  padding: 5px 10px;
}
.table_status_box:hover {
  background: red;
}
.popover_hint {
  background: red;
}
.operation_button_box {
  padding: 0px;
}
.operation_button_details {
  width: 74px;
  height: 32px;
}
.operation_button_release {
  width: 74px;
  height: 32px;
  margin-left: 10px;
  border: 1px #56ced1 solid;
}
.operation_button_release span {
  color: #56ced1;
}
.operation_button_release:hover {
  background: #c9eced;
  opacity: 0.8;
  border-color: #c9eced;
}

.operation_button_passwd {
  width: 74px;
  height: 32px;
  margin-left: 10px;
  border: 0px #eea91c solid;
  background: #eea91c !important;
  color: #fff;
  padding: 0 7px;
  font-size: 14px;
  border-radius: 4px;
  cursor: pointer;
  outline: none;
}
.operation_button_passwd:hover {
  border: 1px #eea91c solid !important;
  background: #eea91c;
  opacity: 0.8;
}
</style>
